package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tservicelog;
import com.xy.manage.service.TservicelogService;
import com.xy.servicemanage.model.ServiceSetting;



@Controller
public class TservicelogAction{
	private static final Logger log = LoggerFactory.getLogger(TservicelogAction.class);
	public TservicelogAction() {
	}
 
 
 	/**
	 * 接口调用记录新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tservicelogAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tservicelog", new Tservicelog());
        setDict(model);
		return "tservicelogEdit";
	}

	/**
	 * 接口调用记录新增保存
	 * 
	 * @param tservicelog
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tservicelogSave.do")
	public String save(Tservicelog tservicelog, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tservicelog, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tservicelog = tservicelogService.save(tservicelog);
		return "redirect:v_tservicelogList.do";
	}

	/**
	 * 接口调用记录删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tservicelogDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tservicelogService.remove(id);
			}
		}
		return "redirect:v_tservicelogList.do";
	}

	/**
	 * 接口调用记录编辑页面
	 * 
	 * @param logid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tservicelogEdit.do")
	public String edit(long logid, HttpServletRequest request, ModelMap model) {
		Tservicelog tservicelog = tservicelogService.get(logid);
		model.addAttribute("tservicelog", tservicelog);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tservicelogEdit";
	}

	/**
	 * 接口调用记录详情
	 * 
	 * @param logid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tservicelogDetail.do")
	public String detail(long logid, HttpServletRequest request, ModelMap model) {
		Tservicelog tservicelog = tservicelogService.get(logid);
		model.addAttribute("tservicelog", tservicelog);
        setDict(model);
		return "tservicelogView";
	}

	/**
	 * 接口调用记录查询（分页）
	 * 
	 * @param tservicelog
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tservicelogList.do")
	public String listPage(Tservicelog tservicelog, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tservicelogService.getTservicelogByPage(toMap(tservicelog),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tservicelogList";
	}

	/**
	 * 接口调用记录查询
	 * 
	 * @param tservicelog
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tservicelogList1.do")
	public String list(Tservicelog tservicelog, HttpServletRequest request, ModelMap model) {
		List<Tservicelog> list = tservicelogService.getTservicelog(toMap(tservicelog));
		model.addAttribute("list", list);
        setDict(model);
		return "tservicelogList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tservicelog
	 * @return
	 */	private Map<String,Object> toMap(Tservicelog tservicelog){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tservicelog.getLogid()!=null&&!tservicelog.getLogid().equals("")){
			map.put("logid", tservicelog.getLogid());
		}
		if(tservicelog.getSid()!=null&&!tservicelog.getSid().equals("")){
			map.put("sid", tservicelog.getSid());
		}
		if(tservicelog.getInvokedate()!=null&&!tservicelog.getInvokedate().equals("")){
			map.put("invokedate", tservicelog.getInvokedate());
		}
		if(tservicelog.getSourceip()!=null&&!tservicelog.getSourceip().equals("")){
			map.put("sourceip", tservicelog.getSourceip());
		}
		if(tservicelog.getLoginfo()!=null&&!tservicelog.getLoginfo().equals("")){
			map.put("loginfo", tservicelog.getLoginfo());
		}
		if(tservicelog.getPhoneNo()!=null&&!tservicelog.getPhoneNo().equals("")){
			map.put("phoneNo", tservicelog.getPhoneNo());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		 Map<Long, String> sidMap=ServiceSetting.getInstance().getServiceDictMap();
		 Map<String, String> result = new HashMap<String, String>();
		 for (Long l : sidMap.keySet()) {
			 result.put(l.toString(), sidMap.get(l));
		}
      model.addAttribute("sidMap",result);		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tservicelog tservicelog, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TservicelogService tservicelogService;   
}